
FLASH_TOOL ?= JLINK
# FLASH_TOOL ?= DAPLINK

FLASH_ADDR ?= 0x08000000

FLASH_BIN ?= $(BUILD_DIR)/$(TARGET).bin
FLASH_HEX ?= $(BUILD_DIR)/$(TARGET).hex

ifeq ($(FLASH_TOOL), JLINK)

#######################################
# flash "E:\SEGGER\JLink\JLink.exe" -ExitOnError 1 -device STM32F429IE -Speed 4000 -IF SWD "C:\Users\ADMINI~1\AppData\Local\Temp\iot_download.jlink"
#######################################
JLINK_EXE = D:/SEGGER/JLink/JLink.exe
# JLINK_EXE = D:/Keil_v5/ARM/Segger/JLink.exe
JLINK_FLAGS += -device STM32H750VB
JLINK_FLAGS += -ExitOnError 1
JLINK_FLAGS += -autoconnect 1
JLINK_FLAGS += -Speed 4000
JLINK_FLAGS += -IF SWD 
JLINK_CMD_FILE = project/GCC/download.jlink

flash:
	touch $(JLINK_CMD_FILE)	
	echo -e "r \r\nloadbin \"$(shell ./tools/getcwd.exe)/$(FLASH_BIN)\" $(FLASH_ADDR) \r\ng \r\nr \r\nqc \r\nexit" > $(JLINK_CMD_FILE)
	$(JLINK_EXE) $(JLINK_FLAGS) $(JLINK_CMD_FILE)

else ifeq ($(FLASH_TOOL), DAPLINK)

OCD_EXE = ./tools/OpenOCD/bin/openocd.exe
OCD_INF = ./tools/OpenOCD/scripts/interface/cmsis-dap.cfg
OCD_TARGET = ./tools/OpenOCD/scripts/target/stm32h7x.cfg
OCD_TARGET_QSPI = ./tools/OpenOCD/scripts/target/stm32h7x_qspi.cfg

flash:
	$(OCD_EXE) -f $(OCD_TARGET_QSPI) -c "init; reset init; halt;" -c "flash write_image erase $(FLASH_HEX)" -c "reset; shutdown;"
#	$(OCD_EXE) -f $(OCD_INF) -c "transport select swd" -f $(OCD_TARGET) -c "init; halt; flash write_image erase $(FLASH_HEX); reset; shutdown;"

flash-unlock:
	$(OCD_EXE) -f $(OCD_INF) -c "transport select swd" -f $(OCD_TARGET) -c "init; reset halt; stm32h7x unlock 0; shutdown;"

flash-qspi:
	$(OCD_EXE) -f $(OCD_TARGET_QSPI) -c "init; reset init; halt;" -c "flash write_image erase ./testbin.bin 0x90000000" -c reset -c shutdown

endif
	
